package com.woniuxy.joylife.manager.controller;

import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylife.manager.cascaders.BlockCascader;
import com.woniuxy.joylife.manager.entity.BlockEntity;
import com.woniuxy.joylife.manager.service.BlockService;
import com.woniuxy.joylife.manager.utils.PageUtils;
import com.woniuxy.joylife.manager.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 楼栋信息
 *
 * @author renxiang
 * @email 981405195@qq.com
 * @date 2020-05-13 02:47:54
 */
@RestController
@RequestMapping("manager/block")
public class BlockController {
    @Autowired
    private BlockService blockService;

    @RequestMapping("/addressCascader")
    public R getCascader(){
        List<BlockCascader> cascader = blockService.getCascader();
        return R.ok().put("data",cascader);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("manager:block:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = blockService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{blocId}")
    //@RequiresPermissions("manager:block:info")
    public R info(@PathVariable("blocId") String blocId){
		BlockEntity block = blockService.getById(blocId);

        return R.ok().put("block", block);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("manager:block:save")
    public R save(@RequestBody BlockEntity block) throws Exception {
        block.setBlocId(KeyUtil.generateKey());
		if(!blockService.saveBlockEntity(block)){
		    throw new Exception("插入失败，栋号已存在");
        };
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("manager:block:update")
    public R update(@RequestBody BlockEntity block){
		blockService.updateById(block);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("manager:block:delete")
    public R delete(@RequestBody String[] blocIds){
		blockService.removeByIds(Arrays.asList(blocIds));

        return R.ok();
    }

}
